using System.Collections.Generic;
using UnityEngine;

//分数管理器
public class ScoreManager : MonoBehaviour
{
    public static ScoreManager Instance { get; private set; }

    // 预设体
    public GameObject scorePrefab;

    // 不同分数对应的精灵数组
    public Sprite[] score3000;
    public Sprite[] score5000;
    public Sprite[] score10000;

    // 字典，用于根据分数查找对应的精灵数组
    private Dictionary<int, Sprite[]> scoreDict;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        scoreDict = new Dictionary<int, Sprite[]>
        {
            { 3000, score3000 },
            { 5000, score5000 },
            { 10000, score10000 }
        };
    }

    // 显示分数的方法
    public void ShowScore(Vector3 position, int score)
    {
        // 实例化分数预设体
        GameObject scoreGo = Instantiate(scorePrefab, position, Quaternion.identity);

        // 根据分数获取对应的精灵数组
        Sprite[] scoreArray;
        if (scoreDict.TryGetValue(score, out scoreArray))
        {
            // 随机选择一个精灵
            int index = Random.Range(0, scoreArray.Length);
            Sprite sprite = scoreArray[index];
            
            // 设置SpriteRenderer的sprite属性
            scoreGo.GetComponent<SpriteRenderer>().sprite = sprite;
        }

        // 在1秒后销毁显示的分数对象
        Destroy(scoreGo, 1f);
    }
}
